<?
class Wiki extends Controller {

	private $config_values;

	function __construct() {
		parent::Controller();
		$this->load->model('Wiki_model');
		$this->load->helper('html');
		$this->load->library('Spyc');
		$path = "cfg/config.yaml";
		$this->config_values = $this->spyc->YAMLLoad($path);
	}

	function index() {
		redirect('wiki/view/system:all');
	}

	function view($wiki_word) {

		$data['title'] = $wiki_word;
		$data['tags'] = $this->Wiki_model->get_tags_by_wiki_word($wiki_word);
		$data['wiki_style'] = false;
		$data['style_css'] = $this->Wiki_model->get_current_style();

		$requested_wiki = split(":",$wiki_word); // System Wikis are split on colons.
		$system_wiki = false;
		if (count($requested_wiki) > 1) { // We are a System Wiki
			$data['wiki'] = $this->Wiki_model->get_system_wiki($requested_wiki);
			$system_wiki=true;
		} else { // We are a Regular Wiki 
			$data['wiki'] = $this->Wiki_model->get_wiki_by_wiki_word($wiki_word);
			$this->Wiki_model->increment_click($wiki_word);
		}
		
		if (isset($data['wiki']['wiki_style']) && ($data['wiki']['wiki_style'])) {
			$data['wiki_style'] = true;
		}
		$data['wiki'] = $this->Wiki_model->prepare_wiki($data['wiki']);
		
		$this->load->library('authenticate');
		$data['is_editable'] = "";
		if (!($data['author'] = $this->authenticate->is_authenticated())) {
			$data['logged_in'] = false;
			$data['bread_crumbs'] = array();
		} else {
			if (!$system_wiki) {
				$data['is_editable'] = "editable";
			}
			$data['logged_in'] = true;
			$data['bread_crumbs'] = $this->Wiki_model->add_bread_crumbs($wiki_word,$data['author'],$this->config_values['max_bread_crumbs']);
		}

		$this->load->helper('form'); //TODO: Is this being used?
		$this->load->view('wiki',$data);
	}

	function create() {
		if ($wiki_word = $this->input->post('wiki_word',TRUE)) {
			redirect('wiki/view/'.$wiki_word);
		} else {
			redirect('wiki');
		}
	}

}

?>
